# 完美校园 数据包
import requests
import demjson
import time


class wmxy():
    username = ''
    securitycode = ''
    cookies = ''
    errmsg = ''
    userInfo = {}

    def __init__(self, username, securitycode, cookies={}) -> None:
        self.username = username
        self.securitycode = securitycode
        if not cookies:
            self.cookies = self.init()

    def init(self):
        headers = {
            'authority': 'reported.17wanxiao.com',
            'cache-control': 'max-age=0',
            'dnt': '1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-user': '?1',
            'sec-fetch-dest': 'document',
            'referer': 'https://reported.17wanxiao.com/index.html',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
        }

        response = requests.get('https://reported.17wanxiao.com/login.html', headers=headers)

        headers = {
            'authority': 'reported.17wanxiao.com',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'dnt': '1',
            'accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'no-cors',
            'sec-fetch-dest': 'image',
            'referer': 'https://reported.17wanxiao.com/login.html',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        }

        params = (
            ('t', '13'),
        )

        response = requests.get('https://reported.17wanxiao.com/captcha.jpg', headers=headers, params=params,
                                cookies=response.cookies)
        return response.cookies

    def login(self):
        headers = {
            'authority': 'reported.17wanxiao.com',
            'accept': '*/*',
            'dnt': '1',
            'x-requested-with': 'XMLHttpRequest',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'origin': 'https://reported.17wanxiao.com',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'referer': 'https://reported.17wanxiao.com/login.html',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
        }

        data = {
            'username': self.username,
            'securitycode': self.securitycode,
            'captcha': ''
        }

        response = requests.post('https://reported.17wanxiao.com/admin/login', headers=headers, data=data,
                                 cookies=self.cookies)
        res = demjson.decode(response.text)
        if not res['result']:
            self.errmsg = res['msg']
            return False
        else:
            self.cookies.set("SERVERID", response.cookies.get("SERVERID"))
            return True

    def getUserInfo(self):
        headers = {
            'authority': 'reported.17wanxiao.com',
            'accept': 'application/json, text/javascript, */*; q=0.01',
            'dnt': '1',
            'x-requested-with': 'XMLHttpRequest',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'referer': 'https://reported.17wanxiao.com/index.html',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        }

        response = requests.get('https://reported.17wanxiao.com/sys/user/info', headers=headers, cookies=self.cookies)
        print(response.text)
        res = demjson.decode(response.text)
        if not res['result']:
            self.errmsg = res['msg']
            return False
        else:
            self.cookies.set("SERVERID", response.cookies.get("SERVERID"))
            self.userInfo = res['user']
            return True

    # 获取未上报名单
    def list2(self, deptId):
        headers = {
            'authority': 'reported.17wanxiao.com',
            'accept': 'application/json, text/javascript, */*; q=0.01',
            'dnt': '1',
            'x-requested-with': 'XMLHttpRequest',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'referer': 'https://reported.17wanxiao.com/sys/unreported2.0.html',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
        }

        params = (
            ('deptId', deptId),
            ('undo', '0'),
            ('reportTime', time.strftime('%Y-%m-%d', time.localtime(time.time()))),
            ('_search', 'false'),
            ('nd', '1611248741845'),
            ('limit', '1000'),
            ('page', '1'),
            ('sidx', ''),
            ('order', 'asc'),
            ('grade', 'null'),
            ('feature', ''),
            ('name', ''),
            ('_', '1611248269302'),
        )

        response = requests.get('https://reported.17wanxiao.com/student/list2', headers=headers, params=params,
                                cookies=self.cookies)
        res = demjson.decode(response.text)
        if not res['result']:
            self.errmsg = res['msg']
            return []
        else:
            self.cookies.set("SERVERID", response.cookies.get("SERVERID"))
            return res['page']['records']

    # 获取已上报名单
    def listObject(self, deptId):
        headers = {
            'authority': 'reported.17wanxiao.com',
            'accept': 'application/json, text/javascript, */*; q=0.01',
            'dnt': '1',
            'x-requested-with': 'XMLHttpRequest',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75',
            'content-type': 'application/x-www-form-urlencoded',
            'origin': 'https://reported.17wanxiao.com',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'referer': 'https://reported.17wanxiao.com/sys/userUpdataInfo',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        }

        data = {
            'customerid': '',
            'customerName': '',
            'deptid': deptId,
            'deptName': '',
            'templateid': 'pneumonia',
            'gpsType': '',
            'reportdateStr': time.strftime('%Y-%m-%d', time.localtime(time.time())),
            'stuNo': '',
            'username': '',
            'customParams[province]': '',
            'customParams[city]': '',
            'customParams[sfwz2]': '',
            'customParams[isConfirmed]': '',
            'customParams[isdefinde]': '',
            'customParams[jtcy]': '',
            'customParams[isGoWarningAdress]': '',
            'customParams[isTouch]': '',
            'customParams[isTransitArea]': '',
            'customParams[isTransitProvince]': '',
            'customParams[isFFHasSymptom]': '',
            'pageSize': '1000',
            'pageNum': '1',
            'orderByColumn': 'stuNo',
            'isAsc': 'asc'
        }

        response = requests.post('https://reported.17wanxiao.com/sys/userUpdataInfo/listObject', headers=headers,
                                 data=data, cookies=self.cookies)
        res = demjson.decode(response.text)
        if not res['code'] == 0:
            return []
        else:
            self.cookies.set("SERVERID", response.cookies.get("SERVERID"))
            return res['rows']
